package com.ybkj.daijia.server.event.listener.zhuanxian;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhaunxianOrderArriveEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import com.ybkj.daijia.server.zhuanxian.Order;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ZhaunxianOrderArriveEventListener implements
    ApplicationListener<ZhaunxianOrderArriveEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhaunxianOrderArriveEventListener.class);


    @Autowired
    private MemberService memberService;

    @Autowired
    private ZhuanxianService zhuanxianService;

    @Autowired
    private SettingUtils settingUtils;

    @Override
    @Async
    public void onApplicationEvent(ZhaunxianOrderArriveEvent event) {

        Long orderId = (Long) event.getSource();
        Order order = zhuanxianService.findOne(orderId);

        Passenger passenger = memberService.findOnePassenger(order.getPassengerId());

        passenger.setCid(memberService.findCid(passenger.getId()));

        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                String address = "";
                if (order != null) {
                    address = order.getStartAddress();
                }
                PushData pushData = new PushData();
                pushData.setType("WAIT_ORDER_ZHUANXIAN");
                pushData.setTitle("服务人员到达预约地");
                pushData.setDescription("服务人员已到达预约地【" + address + "】");
                pushData.setData(order.getId());
                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                e.getMessage();
            }

        }

    }

}
